﻿Imports DevComponents.AdvTree
Imports DevComponents.Editors.DateTimeAdv
Imports DevComponents.DotNetBar.Controls
Imports HTLibrary
Imports TNCBAL
Imports TNCDAL
Imports Excel = Microsoft.Office.Interop.Excel

Partial Public Class CViewer
    Public Shared Sub BindingMultiColumnComboBox_Customer(ByVal cbo As MultiColumnComboBox, ByVal isExport As Boolean, ByVal maxWidth As Integer)
        Dim objCustomerController As New CustomerController
        Dim query = (From p In objCustomerController.SelectAllRecords()
                    Select p.ID_Customer, p.CustomerName, p.tblCompany.Name Order By CustomerName Ascending).ToList()
        'Lấy tên Customer max length
        Dim cusNameList = (From p In query
                                Select p.CustomerName
                                Order By CustomerName.Length Descending).First()
        'Lấy tên Company max length
        Dim comNameList = (From p In query
                                Select p.Name
                                Order By Name.Length Descending).First()
        'Tính kích thước pixel
        Dim len, lenCom As Integer
        Using graphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(New Bitmap(1, 1))
            Dim size As SizeF = graphics.MeasureString(cusNameList, New Font(cbo.Font.Name, cbo.Font.Size + 1, FontStyle.Regular, GraphicsUnit.Point))
            len = size.Width

            size = graphics.MeasureString(comNameList, New Font(cbo.Font.Name, cbo.Font.Size + 1, FontStyle.Regular, GraphicsUnit.Point))
            lenCom = size.Width
        End Using

        Dim db As New DataTable()
        db = CollectionHelper.ConvertTo(query)
        'cbo.Width = len
        cbo.ColumnWidths = String.Format("0;{0};{1}", len, lenCom)
        cbo.ValueMember = "ID_Customer"
        cbo.DisplayMember = "CustomerName"
        cbo.DataSource = db
    End Sub

    Public Shared Sub BindingComboBox_ForeignCompany(ByVal cbo As ComboBoxEx, ByVal isExport As Boolean)
        cbo.DataSource = Nothing
        '1. ForeignCompany là Consignee
        Dim objForeignCompanyRepository As New CForeignCompany(GeneralClassDAL.dc)
        Dim queryConsignee = objForeignCompanyRepository.SelectAllRecord(isExport)
        'cbo.DropDownStyle = ComboBoxStyle.DropDown
        cbo.ValueMember = "ID_ForeignCompany"
        cbo.DisplayMember = "Name"
        cbo.DataSource = queryConsignee

        cbo.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        cbo.AutoCompleteSource = AutoCompleteSource.ListItems

        'Dim source = New AutoCompleteStringCollection()
        'Dim itemList = From p In queryConsignee
        '            Select p.Name
        'source.AddRange(itemList.ToArray())
        'cbo.AutoCompleteMode = AutoCompleteMode.Suggest
        'cbo.AutoCompleteSource = AutoCompleteSource.CustomSource
        'cbo.AutoCompleteCustomSource = source
    End Sub

    Public Shared Sub BindingComboBox_Company(ByVal cbo As ComboBoxEx, ByVal isExport As Boolean)
        cbo.DataSource = Nothing
        Dim objCompanyController As New CompanyController
        Dim queryShipper = objCompanyController.SelectAllRecord()
        'cbo.DropDownStyle = ComboBoxStyle.DropDown
        cbo.ValueMember = "ID_Company"
        cbo.DisplayMember = "Name"
        cbo.DataSource = queryShipper
        cbo.SelectionLength = 0

        cbo.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        cbo.AutoCompleteSource = AutoCompleteSource.ListItems


        'Dim source = New AutoCompleteStringCollection()
        'Dim itemList = From p In queryShipper
        '            Select p.Name
        'source.AddRange(itemList.ToArray())
        'cbo.AutoCompleteMode = AutoCompleteMode.Suggest
        'cbo.AutoCompleteSource = AutoCompleteSource.CustomSource
        'cbo.AutoCompleteCustomSource = source
    End Sub

    Public Shared Sub BindingComboBox_Agent(ByVal cbo As ComboBoxEx)
        cbo.DataSource = Nothing
        '2. Danh sách Agent
        Dim objAgentRepository As New CAgent
        cbo.ValueMember = "ID_Agent"
        cbo.DisplayMember = "Name"
        Dim queryAgent = objAgentRepository.SelectAllRecord()
        cbo.DataSource = queryAgent
        cbo.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        cbo.AutoCompleteSource = AutoCompleteSource.ListItems
    End Sub

    ''' <summary>
    ''' Danh sách các cảng Việt Nam
    ''' </summary>
    ''' <param name="cbo"></param>
    ''' <remarks></remarks>
    Public Shared Sub BindingComboBox_VietNamPort(ByVal cbo As ComboBoxEx)
        Dim objPortController As New CPortController(GeneralClassDAL.dc)
        cbo.ValueMember = "ID_Port"
        cbo.DisplayMember = "Port"
        Dim queryNoiHaCont = (From p In objPortController.SelectAllRecord()
                                 Where p.ID_Country = 1
                                 Select p).OrderBy(Function(p) p.Port)
        cbo.DataSource = queryNoiHaCont
        cbo.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        cbo.AutoCompleteSource = AutoCompleteSource.ListItems
    End Sub

    ''' <summary>
    ''' Danh sách các cảng Việt Nam
    ''' </summary>
    ''' <param name="cbo"></param>
    ''' <remarks></remarks>
    Public Shared Sub BindingMultiColumnComboBox_VietNamPort(ByVal cbo As MultiColumnComboBox)
        Dim objPortController As New CPortController(GeneralClassDAL.dc)
        Dim query = (From p In objPortController.SelectAllRecord()
                                 Where p.ID_Country = 1
                                 Select p).OrderBy(Function(p) p.Port)

        Dim lenPort As Integer = (From p In query
                                Select p.Port).Max().Length * 15
        Dim lenCountry As Integer = (From p In query
                                Select p.tblCountry.CountryName).Max().Length * 10
        Dim db As New DataTable
        db = CollectionHelper.ConvertTo(query.ToList())
        'cbo.Width = len
        cbo.ColumnWidths = String.Format("0;{0};{1}", lenPort, lenCountry)
        cbo.DataSource = db
        cbo.ValueMember = "ID_Port"
        cbo.DisplayMember = "Port"
    End Sub
    ''' <summary>
    ''' Danh sách các cảng nước ngoài
    ''' </summary>
    ''' <param name="cbo"></param>
    ''' <remarks></remarks>
    Public Shared Sub BindingComboBox_ForeignPort(ByVal cbo As ComboBoxEx)
        Dim objPortController As New CPortController(GeneralClassDAL.dc)
        cbo.ValueMember = "ID_Port"
        cbo.DisplayMember = "Port"
        Dim queryNoiHaCont = (From p In objPortController.SelectAllRecord()
                                 Where p.ID_Country <> 1
                                 Select p).OrderBy(Function(p) p.Port)
        cbo.DataSource = queryNoiHaCont
        cbo.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        cbo.AutoCompleteSource = AutoCompleteSource.ListItems
    End Sub

    ''' <summary>
    ''' Danh sách các hãng tàu
    ''' </summary>
    ''' <param name="cbo"></param>
    ''' <remarks></remarks>
    Public Shared Sub BindingComboBox_Line(ByVal cbo As ComboBoxEx)
        cbo.ValueMember = "ID_Line"
        cbo.DisplayMember = "LineName"
        Dim objLine = New CLine(GeneralClassDAL.dc)
        Dim queryLine = (From p In objLine.SelectAllRecord()
                             Select p).OrderBy(Function(p) p.LineName)
        cbo.DataSource = queryLine
        cbo.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        cbo.AutoCompleteSource = AutoCompleteSource.ListItems
    End Sub

    Public Shared Sub BindingComboBox_Vessel(ByVal cbo As ComboBoxEx)
        cbo.ValueMember = "ID_Vessel"
        cbo.DisplayMember = "VesselName"
        Dim objVessel = New CVessel(GeneralClassDAL.dc)
        Dim queryVessel = (From q In objVessel.SelectAllRecord()
                               Select q).OrderBy(Function(p) p.VesselName)
        cbo.DataSource = queryVessel
        cbo.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        cbo.AutoCompleteSource = AutoCompleteSource.ListItems
    End Sub

    ''' <summary>
    ''' Danh sách currency khác VND
    ''' </summary>
    ''' <param name="cbo"></param>
    ''' <remarks></remarks>
    Public Shared Sub BindingComboBox_OtherCurrency(ByVal cbo As ComboBoxEx)
        cbo.ValueMember = "ID_Currency"
        cbo.DisplayMember = "Code"
        Dim objCurrency = New MCurrencies
        cbo.DataSource = From p In objCurrency.SelectAllRecord()
                                 Where p.ID_Currency <> 1
                                 Select p
        cbo.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        cbo.AutoCompleteSource = AutoCompleteSource.ListItems
    End Sub

    ''' <summary>
    ''' In phiếu tạm ứng
    ''' </summary>
    ''' <param name="id_advancevoucher"></param>
    ''' <remarks></remarks>
    Public Shared Sub PrintReport_AdvanceVoucher(ByVal id_advancevoucher As Integer)

        Dim rpt As New rptAdvanceVoucher
        Dim query2 = (From p In GeneralClassDAL.dc.tblFileFees
                Where p.ID_AdvanceVoucher = id_advancevoucher
                Select New With {
                    p.tblFee.FeeName, p.Price, p.ID_AdvanceVoucher
                                                     }).ToList()


        If query2.Count > 0 Then
            Dim fileNoQuery = (From p In GeneralClassDAL.dc.tblFileFees
                Where p.ID_AdvanceVoucher = id_advancevoucher
                Select p.tblFile).Distinct().Take(1)
            Dim objFile As tblFile
            For Each q In fileNoQuery
                objFile = q
            Next
            rpt.SetDataSource(HTLibrary.CollectionHelper.ConvertTo(query2))
            Dim fileNo As String
            If objFile.IsExport Then
                fileNo = objFile.FileNo.ToString() + "/EX"
            Else
                fileNo = objFile.FileNo.ToString() + "/IM"
            End If
            rpt.SetParameterValue("datePrint", Now.Date)
            rpt.SetParameterValue("fileNO", fileNo)
            rpt.SetParameterValue("contNO", "")
            frmMain.AddNewDockContainerItem("PrintAdvanceVoucher", "Print Advance Voucher File " + fileNo, New ucReportViewer(rpt))
        End If
    End Sub

    ''' <summary>
    ''' Hàm lấy giá trị số lượng từ textbox control
    ''' </summary>
    ''' <param name="txt"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetNumberValue_TextBox(ByVal txt As TextBoxX) As Double
        Dim value As Double
        Try
            value = Double.Parse(txt.Text)
        Catch ex As Exception
            txt.Text = ""
            value = 0
        End Try
        Return value
    End Function

    ''' <summary>
    ''' Cho phép reset toàn bộ các control về giá trị mặc định
    ''' Dùng cho những form không sử dụng ucCommands
    ''' </summary>
    ''' <param name="controlCollect"></param>
    ''' <remarks></remarks>
    Public Shared Sub ResetControls(ByRef controlCollect As Control.ControlCollection)
        For Each c As Control In controlCollect
            If c.Controls.Count > 0 Then
                ResetControls(c.Controls)
            End If

            'Visual Studio Controls
            If TypeOf c Is TextBox Then
                CType(c, TextBox).Text = ""
            ElseIf TypeOf c Is RichTextBox Then
                CType(c, RichTextBox).Text = ""
            ElseIf (TypeOf c Is DateTimeInput) Then
                CType(c, DateTimeInput).Value = Nothing
            ElseIf (TypeOf c Is CheckBox) Then
                CType(c, CheckBox).CheckState = CheckState.Indeterminate
            End If

            'DotNetBar Controls
            If TypeOf c Is TextBoxX Then
                CType(c, TextBoxX).Text = ""
            ElseIf (TypeOf c Is ComboTree) Then
                'CType(c, ComboTree).Se
            ElseIf (TypeOf c Is ComboBoxEx) Then
                CType(c, ComboBoxEx).Text = ""
                CType(c, ComboBoxEx).SelectedIndex = -1
            ElseIf (TypeOf c Is CheckBoxX) Then
                CType(c, CheckBoxX).CheckState = CheckState.Indeterminate
            End If
        Next
    End Sub

#Region "Format DataGridView"
    ''' <summary>
    ''' Tìm giá trị tại vị trí column cụ thể
    ''' </summary>
    ''' <param name="gridView"></param>
    ''' <param name="columnName"></param>
    ''' <param name="someText"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetIndexRowInDataGridViewByTextSearch(ByRef gridView As DataGridViewX, ByVal columnName As String, ByVal someText As String) As Integer
        Dim gridRow As Integer = 0
        Dim rowIndex As Integer = 0
        For Each Row As DataGridViewRow In gridView.Rows
            Dim cell As DataGridViewCell = Row.Cells(columnName)
            If cell.Value.ToString.ToLower = someText.ToLower Then
                Return Row.Index
            End If
        Next Row
        Return -1
    End Function

    ''' <summary>
    ''' Format datagridview
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Sub FormatDataGridView(ByRef gridView As DataGridViewX, _
                                         Optional ByVal hideColumns As List(Of String) = Nothing, _
                                         Optional ByVal renameColumns As Dictionary(Of String, String) = Nothing, _
                                         Optional ByVal frozenColumnName As String = Nothing, _
                                         Optional ByVal numberColumns As List(Of String) = Nothing)
        'Hide column
        If hideColumns IsNot Nothing Then
            For Each columnName As String In hideColumns
                gridView.Columns(columnName).Visible = False
            Next
        End If

        'Forzen column
        If frozenColumnName IsNot Nothing Then
            For Each col As DataGridViewColumn In gridView.Columns
                gridView.Columns(col.Name).Frozen = True
                If col.Name = frozenColumnName Then
                    Exit For
                End If
            Next
        End If

        'Quantity column
        If numberColumns IsNot Nothing Then
            For Each columnName As String In numberColumns
                gridView.Columns(columnName).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                gridView.Columns(columnName).DefaultCellStyle.Format = CollectionHelper.FORMAT_QUANTITY
            Next
        End If

        'Rename Columns
        If renameColumns IsNot Nothing Then
            For Each columnName As KeyValuePair(Of String, String) In renameColumns
                gridView.Columns(columnName.Key).HeaderText = columnName.Value
            Next
        End If

        gridView.AllowUserToAddRows = False
        gridView.AllowUserToDeleteRows = False
        gridView.AllowUserToResizeColumns = True
        gridView.ReadOnly = True
        gridView.SelectionMode = DataGridViewSelectionMode.CellSelect
        gridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
        gridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
    End Sub

    Public Shared Sub BindingAdvTreeView_Drivers(ByVal advTree As AdvTree)
        Dim ctrlDriver As New MDrivers(GeneralClassDAL.dc)
        Dim listDrivers As IQueryable(Of tblDriver) = ctrlDriver.getDriversList2
        advTree.DataSource = listDrivers
        advTree.ValueMember = "ID_Driver"
        advTree.DisplayMembers = "DriverName"
    End Sub
#End Region

End Class
